package nju.se.alibaba.dao;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nju.se.alibaba.model.Tmall;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class AlibabaDAO {
	public static SqlSession session;

	public AlibabaDAO() {
		getSqlSession();
	}

	private static SqlSession getSqlSession() {
		String resource = "mybatis-config.xml";
		InputStream inputStream = null;
		try {
			inputStream = Resources.getResourceAsStream(resource);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		session = new SqlSessionFactoryBuilder().build(inputStream)
				.openSession();
		return session;

	}
	
	public static void closeSqlSession(){
		session.close();
	}

	public static String getCountAll() {

		try {
			int count = session.selectOne("TmallMapper.countAll");
			return count + "";
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 

	}

	public static List<Tmall> getPeriodAlgorithm(int i) {

		try {
			List<Tmall> result = session.selectList(
					"TmallMapper.getPeriodAlgorithm", i);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	public static List<Integer> getAllUser() {
		try {
			List<Integer> result = session.selectList("TmallMapper.getAllUser");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static List<Integer> getOnlyType0RecordByUser(int user_id) {
		try {
			List<Integer> result = session.selectList(
					"TmallMapper.getOnlyType0RecordByUser", user_id);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 

	}

	public static List<Integer> predictOnlyType0RecordByUser(int user_id,
			int base_line) {
		try {
			Map map = new HashMap<String, Integer>();
			map.put("user_id", user_id);
			map.put("base_line", base_line);
			List<Integer> result = session.selectList(
					"TmallMapper.predictOnlyType0RecordByUser", map);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
	}

	public static List<Integer> getOnlyType2RecordByUser(int user_id) {
		try {
			List<Integer> result = session.selectList(
					"TmallMapper.getOnlyType2RecordByUser", user_id);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}  
	}

	public static List<Integer> predictOnlyType2RecordByUser(int user_id) {
		try {
			List<Integer> result = session.selectList(
					"TmallMapper.predictOnlyType2RecordByUser", user_id);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
	}
	
	public static List<Integer> getOnlyType3RecordByUser(int user_id) {
		try {
			List<Integer> result = session.selectList(
					"TmallMapper.getOnlyType3RecordByUser", user_id);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}  
	}

	public static List<Integer> predictOnlyType3RecordByUser(int user_id) {
		try {
			List<Integer> result = session.selectList(
					"TmallMapper.predictOnlyType3RecordByUser", user_id);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
	}
	
	public static List<Integer> getType2Type3RecordByUser(int user_id) {
		try {
			List<Integer> result = session.selectList(
					"TmallMapper.getType2Type3RecordByUser", user_id);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}  
	}

	public static List<Integer> predictType2Type3RecordByUser(int user_id) {
		try {
			List<Integer> result = session.selectList(
					"TmallMapper.predictType2Type3RecordByUser", user_id);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
	}

	public static List<Tmall> getBrandIdListByUserId() {
		List<Tmall> result = new ArrayList<Tmall>();
		try {
			result = session.selectList("TmallMapper.getBrandIdListByUserId");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
	}

	public static List<Tmall> getBrandIdListByUserIdByMonth(int start, int end) {
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		map.put("start", start);
		map.put("end", end);
		List<Tmall> result = new ArrayList<Tmall>();
		try {
			result = session.selectList(
					"TmallMapper.getBrandIdListByUserIdByMonth", map);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
	}

	@SuppressWarnings("rawtypes")
	public static List<Tmall> getPreBrand(Map map) {
		List<Tmall> result = new ArrayList<Tmall>();
		try {
			result = session.selectList("TmallMapper.getPreBrand", map);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
	}

}
